package com.hbjycl.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.hbjycl.dao.AdminDao;
import com.hbjycl.entity.Admin;
import com.hbjycl.entity.Role;


@Repository
@Transactional
public class AdminDaoImpl extends BaseDaoImpl<Admin, String> implements AdminDao
{
    @Override
    public boolean isExistByUsername(String username)
    {
        String hql = "from Admin admin where  admin.username  = ?";
        Admin admin = (Admin) getSession().createQuery(hql).setParameter(0, username)
                .uniqueResult();
        if (admin != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    @Override
    public Admin getAdminByUsername(String username)
    {
        String hql = "from Admin admin where admin.username = ?";
        return (Admin) getSession().createQuery(hql).setParameter(0, username).uniqueResult();
    }
    
    @Override
    public List<Role> getRoleSet(String username)
    {
        String hql = "select roleSet from Admin where username=?";
        return getSession().createQuery(hql).setParameter(0, username).list();
    }
    
       
    
    @Override
    public boolean deleteUserRole(String userId)
    {
        String sql = "delete from admin_role where adminset_id=?";
        int result = getSession().createSQLQuery(sql).setParameter(0, userId).executeUpdate();
        return result > 0;
    }

    @Override
    public List<Admin> getLockedAccounts()
    {
        String hql = "from Admin where isAccountLocked=?";
        return getSession().createQuery(hql).setParameter(0, true).list();
    }

}
